package com.homestead.controller;

import com.homestead.bo.CouponThemeConditionBo;
import com.homestead.bo.CouponThemeCreateBo;
import com.homestead.controller.req.CouponThemeConditionRequest;
import com.homestead.controller.req.CouponThemeCreateRequest;
import com.homestead.controller.resp.CouponThemeConditionVo;
import com.homestead.domain.dos.CouponThemeCache;
import com.homestead.domain.entity.Page;
import com.homestead.resp.CommonResponse;
import com.homestead.service.CouponThemeCacheService;
import com.homestead.service.CouponThemeService;
import com.homestead.utils.CommonResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.Set;

/**
 * @author yhb
 * @date 2021-04-09 11:43
 */
@RestController
@RequestMapping("/couponTheme")
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
@Api(tags = {"券主题基础信息"})
public class CouponThemeController {
    private final CouponThemeService couponThemeService;

    private final CouponThemeCacheService couponThemeCacheService;

    @PostMapping("/list")
    @ApiOperation(value = "列表", httpMethod = "POST")
    public CommonResponse<Page<CouponThemeConditionVo>> list(@RequestBody CouponThemeConditionRequest in) {
        CouponThemeConditionBo bo = in.convert();

        Page<CouponThemeConditionVo> page = couponThemeService.list(bo);

        return CommonResponseUtil.successObj(page);
    }

    @PostMapping("/create")
    @ApiOperation(value = "创建券主题配置", consumes = "application/json")
    public CommonResponse<Void> create(@RequestBody CouponThemeCreateRequest in) {
        CouponThemeCreateBo bo = in.convert();
        couponThemeService.create(bo);
        return CommonResponseUtil.success();
    }

    @PostMapping("/getById")
    @ApiOperation(value = "获取单个券主题配置", consumes = "application/json")
    public CommonResponse<CouponThemeCache> getById(Long couponThemId) {
        CouponThemeCache theme = couponThemeCacheService.getById(couponThemId);
        return CommonResponseUtil.successObj(theme);
    }

    @PostMapping("/getSelectedFieldsById")
    @ApiOperation(value = "获取单个券主题配置字段", consumes = "application/json")
    public CommonResponse<CouponThemeCache> getSelectedFieldsById(Long couponThemId, String field) {
        Set<CouponThemeCache.FIELDS> set = new HashSet<>();
        set.add(CouponThemeCache.FIELDS.of(field));

        CouponThemeCache theme = couponThemeCacheService.getSelectedFieldsById(couponThemId, set);
        return CommonResponseUtil.successObj(theme);
    }
}
